<template>
  <div>
    <!-- 渲染角色列表 -->
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="queding">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getEmployeeInformation } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    // 用户的id 用来查询当前用户的角色信息
    roles: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      roleIds: [], // 选中的复选框
      roleList: [] //  角色的列表
    }
  },
  created() {
    this.getRoleList()
    //  通过id获得角色列表
    this.getEmployeeInformation()
  },
  methods: {
    // 取消弹框
    cancleDialog() {
      this.$emit('close')
    },
    async getRoleList() {
      const res = await getRoleList()

      this.roleList = res.data.rows
    },
    // 通过id获得角色列表
    async getEmployeeInformation() {
      const res = await getEmployeeInformation(this.roles)
      this.roleIds = res.data.roleIds
    },
    // 点击确定
    async queding() {
      const res = await assignRoles({ roleIds: this.roleIds, id: this.roles })
      console.log(res)
      this.getRoleList()
      this.$message.success(res.message)
      this.$emit('quedingguanbi')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
